<?php

class search_indexModel extends Object{

    public function __construct() {
        $this->LoadModel('search/palavra', 'dic');
        $this->LoadModel('search/dicionario', 'dicdoc');
        $this->LoadModel('search/documento', 'doc');
        $this->LoadResource("database", 'db');
    }
    
    public function search($search, $page){
        require_once 'StemmingAlgorithm.php';
        $st = new StemmingAlgorithm();
        
        $search = explode(" ", $search);
        foreach($search as &$s){
            $s = $st->execute($s);
            $s = str_replace(array("'", " "), array('"', ""), $s);
            $s = "palavra like '$s%'";
        }
        $where = implode(" OR ", $search);
        $sql = "
        SELECT d.*
        FROM search_documento d
        WHERE cod_documento in(
            SELECT cod_documento
            FROM search_dicdoc dd
            NATURAL JOIN search_dicionario dic
            WHERE $where
            GROUP BY cod_documento
            ORDER BY dd.numocorrencias DESC
        )
        
        ";
        //echo $sql;
        return $this->db->ExecuteQuery($sql);
    }
    
    public function indexar($modelname, $dados, $pkey, $post){
        $dados = $this->md->getDados();
        $pkey = $this->md->getPkey();
        foreach($dados as $name => $arr){
            if(!array_key_exists("search", $arr)) continue;
            $search[] = $arr['search'];
        }
        print_r($search);
    }
    
}

?>